//
//  AppDelegate.m
//  UI01_UIWindow_UIView
//
//  Created by wyx on 16/3/9.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "MyView.h"
#import "MyButton.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

-(void)dealloc{
    [_window release];
    [super dealloc];
}

#pragma mark - 知识点1:UIWindow
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //创建 window 对象
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];//大小主屏幕
    self.window.backgroundColor = [UIColor whiteColor];
    //指定 Windows 的根视图控制器
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    //指定 windo 为主 Windows 并可见
    [self.window makeKeyAndVisible];
    
    /** 知识点2 */
    [self cresteView];
    
    /** 知识点3 */
    [self managingtheViewHierarchy];
    
    /** 知识点4 */
    [self createButton];
    //内存 管理
    [_window release];
    [vc release];
    return YES;
}
#pragma mark - 知识点2: UIView
- (void)cresteView{
    
    //viewGreen
    MyView *viewOfGreen = [[MyView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];//Frame 属性是相对于父视图坐标系
    viewOfGreen.backgroundColor = [UIColor greenColor];
    [self.window addSubview:viewOfGreen];
    [viewOfGreen release];
    
    
    //viewOfRed
    MyView *viewOfRed = [[MyView alloc] initWithFrame:CGRectMake(30, 30, 40, 40)];
    viewOfRed.backgroundColor = [UIColor blackColor];
    [viewOfGreen addSubview:viewOfRed];
    [viewOfRed release];
    
    //viewOfYellow(使用自定义初始化方法,含有color 参数)
    MyView *viewOfYellow = [[MyView alloc] initWithFrame:CGRectMake(150, 150, 100, 100) backgroundColor:[UIColor yellowColor]];
    [self.window addSubview:viewOfYellow];
    [viewOfYellow release];
    
    //常用 API
    viewOfRed.center = CGPointMake(200, 200);//中心点
    
    viewOfRed.hidden = NO;//隐藏
    
    viewOfRed.alpha = 0.5;//透明度
    
    viewOfRed.tag = 100;//加标签
    viewOfYellow.tag = 300;
    viewOfGreen.tag = 200;
}
#pragma mark - 知识点3: View 的图层管理
- (void)managingtheViewHierarchy {
    //根据 tag 找到 viewOfRed 对象
    MyView *viewOfSearch = [self.window viewWithTag:200];
    //查看 viewOfRed 的父视图
    NSLog(@"%@", viewOfSearch.superview);
    //查看 viewOfRed 的子视图
    NSLog(@"%@", viewOfSearch.subviews);
    //查看 window 子视图
    NSLog(@"%@",self.window.subviews);
    //把 viewOfRed放在上面
    [self.window bringSubviewToFront:viewOfSearch];
}
#pragma mark - 知识点4: UIButton
- (void)createButton {
    //创建对象
    UIButton *buttonOfConfirm = [MyButton buttonWithType:UIButtonTypeSystem];
    buttonOfConfirm.frame = CGRectMake(100, 600, 80, 40);
    buttonOfConfirm.backgroundColor = [UIColor cyanColor];
    [self.window addSubview:buttonOfConfirm];
    //常用 API
    //button 上面的标题
    [buttonOfConfirm setTitle:@"确定" forState: UIControlStateNormal];
    //标题颜色
    [buttonOfConfirm setTintColor:[UIColor yellowColor]];
    //添加点击事件
    //[buttonOfConfirm addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchDown];//down是按下去不松手是就触发
    [buttonOfConfirm addTarget:self action:@selector(handleUP:) forControlEvents:UIControlEventTouchUpInside];
    buttonOfConfirm.tag = 20;
    
    //取消键
    MyButton *buttonOfCencel = [MyButton buttonWithType:UIButtonTypeSystem];
    buttonOfCencel.frame = CGRectMake(50+200, 600, 80, 40);
    [self.window addSubview:buttonOfCencel];
    buttonOfCencel.backgroundColor = [UIColor cyanColor];
    [buttonOfCencel setTitle:@"取消" forState: UIControlStateNormal];
    [buttonOfCencel setTintColor:[UIColor yellowColor]];
    [buttonOfCencel addTarget:self action:@selector(handleUP:) forControlEvents:UIControlEventTouchUpInside];
    buttonOfCencel.tag = 21;
}
//点击 Button 触发的方法的实现
- (void)handleAction:(UIButton *)button {
    NSLog(@"确定");
}
- (void)handleUP:(UIButton *)button {
    //根据 Button 的 tag执行不同的操作
    switch (button.tag) {
        case 20:
            NSLog(@"确定");
            break;
        case 21:
            NSLog(@"取消");
            
        default:
            break;
    }
    
}
#pragma mark - UIApplicationDelegate 其他协议方法
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
